﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Activities;
using Microsoft.TeamFoundation.VersionControl.Client;
using System.IO;
using Microsoft.TeamFoundation.Build.Client;

namespace BDV.TfsCustomActivities.BuildTasks
{
    /// <summary>
    /// Checkout a resource (folder or file)
    /// Checkout d'une resource (répertoire ou fichier)
    /// </summary>
    [BuildActivity(HostEnvironmentOption.All)]
    public sealed class CheckOut : CodeActivity
    {
        /// <summary>
        /// Resource server's path
        /// </summary>
        [RequiredArgument]
        public InArgument<string> ServerItemResourcePath { get; set; }

        /// <summary>
        /// Workspace concerned
        /// </summary>
        [RequiredArgument]
        public InArgument<Workspace> WorkSpace { get; set; }

        // If your activity returns a value, derive from CodeActivity<TResult>
        // and return the value from the Execute method.
        protected override void Execute(CodeActivityContext context)
        {
            // Obtain the runtime value of the Text input argument
            string srvResxPath = context.GetValue(this.ServerItemResourcePath);
            Workspace wkSpace = context.GetValue(this.WorkSpace);

            string localPath = wkSpace.GetLocalItemForServerItem(srvResxPath);

            if ((File.GetAttributes(localPath) & FileAttributes.Directory) == FileAttributes.Directory)
            {
                wkSpace.PendEdit(localPath, RecursionType.Full);
            }
            else
            {
                wkSpace.PendEdit(localPath);
            }
        }
    }
}
